<template>
  <div :style="{ ...coreStyle.marginSame(8) }">
    <a-card>
      <a-form ref="dataSourceFormQueryRef" :model="dataSourceFormQueryModel" labelAlign="left" layout="vertical" :style="{ ...coreStyle.marginBottom(10) }">
        <a-row :gutter="16" v-show="!dataSourceFormQueryVisible">
          <a-col :span="8">
            <a-form-item :style="{ ...coreStyle.marginBottom(-8) }">
              <a-input-search v-model:value="dataSourceFormQueryModel.keySearchVo" placeholder="关键字搜索" :disabled="dataSourceFormQuerySubmitting" @search="dataSourceFormQuerySearch" />
            </a-form-item>
          </a-col>
          <a-col :span="16">
            <a-button type="link" :style="{ ...coreStyle.floatRight }" @click="dataSourceFormQueryUpDown">
              展开
              <CoreIcon :icon="coreStyle.antBtnIconExpand" :size="14" />
            </a-button>
          </a-col>
        </a-row>
        <a-row :gutter="16" v-show="dataSourceFormQueryVisible">
          <a-col :span="8">
            <a-form-item>
              <a-input-search v-model:value="dataSourceFormQueryModel.keySearchVo" placeholder="关键字搜索" :disabled="dataSourceFormQuerySubmitting" @search="dataSourceFormQuerySearch" />
            </a-form-item>
          </a-col>
          <a-col :span="16">
          </a-col>
          <a-col :span="8">
            <a-form-item label="数据源名称">
              <a-input v-model:value="dataSourceFormQueryModel.dataSourceNameAndLike" placeholder="输入数据源名称" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="数据源标识">
              <a-input v-model:value="dataSourceFormQueryModel.dataSourceKeyAndLike" placeholder="输入数据源标识" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="数据库类型">
              <a-input v-model:value="dataSourceFormQueryModel.dbTypeAndLike" placeholder="输入数据库类型" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="IP地址">
              <a-input v-model:value="dataSourceFormQueryModel.ipAddressAndLike" placeholder="输入IP地址" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="端口">
              <a-input v-model:value="dataSourceFormQueryModel.portAndLike" placeholder="输入端口" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="服务名">
              <a-input v-model:value="dataSourceFormQueryModel.serverNameAndLike" placeholder="输入服务名" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="数据库名">
              <a-input v-model:value="dataSourceFormQueryModel.dbNameAndLike" placeholder="输入数据库名" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="用户名">
              <a-input v-model:value="dataSourceFormQueryModel.usernameAndLike" placeholder="输入用户名" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="密码">
              <a-input v-model:value="dataSourceFormQueryModel.passwordAndLike" placeholder="输入密码" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="描述">
              <a-input v-model:value="dataSourceFormQueryModel.descriptionAndLike" placeholder="输入描述" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="排序权重">
              <a-input-number v-model:value="dataSourceFormQueryModel.weightOrder" :style="{ ...coreStyle.width('100%') }" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-space :style="{ ...coreStyle.floatRight }">
              <a-button type="dashed" :style="{ ...coreStyle.antBtnReset }" @click="dataSourceFormQueryReset">
                <CoreIcon :icon="coreStyle.antBtnIconReset" :size="14" />
                重置
              </a-button>
              <a-button :loading="dataSourceFormQuerySubmitting" type="dashed" :style="{ ...coreStyle.antBtnQuery }" @click="dataSourceFormQuerySearch">
                <CoreIcon :icon="coreStyle.antBtnIconQuery" :size="14" />
                查询
              </a-button>
              <a-button type="dashed" ghost :style="{ ...coreStyle.antBtnRetract }" @click="dataSourceFormQueryUpDown">
                收起
                <CoreIcon :icon="coreStyle.antBtnIconRetract" :size="14" />
              </a-button>
            </a-space>
          </a-col>
        </a-row>
      </a-form>
      <a-space>
        <a-button type="dashed" :style="{ ...coreStyle.antBtnAdd }" @click="dataSourceAdd">
          <CoreIcon :icon="coreStyle.antBtnIconAdd" :size="14" />
          新增
        </a-button>
        <a-button v-if="dataSourceListSelectLength === 1" type="dashed" :style="{ ...coreStyle.antBtnUpdate }" @click="dataSourceUpdate">
          <CoreIcon :icon="coreStyle.antBtnIconUpdate" :size="14" />
          修改
        </a-button>
        <a-popconfirm v-if="dataSourceListSelectLength > 0" title="确认删除？" okType="dashed" placement="bottom" @confirm="dataSourceDelete">
          <a-button type="dashed" :style="{ ...coreStyle.antBtnDelete }">
            <CoreIcon :icon="coreStyle.antBtnIconDelete" :size="14" />
            删除
          </a-button>
        </a-popconfirm>
        <a-button v-if="dataSourceListSelectLength === 1" type="dashed" :style="{ ...coreStyle.antBtnDetail }" @click="dataSourceDetailInfo">
          <CoreIcon :icon="coreStyle.antBtnIconDetail" :size="14" />
          详情
        </a-button>
        <a-button v-if="dataSourceListSelectLength === 1" type="dashed" :style="{ 'color': '#389e0d' }" @click="dataSourceAddDataSource">
          <CoreIcon icon="ant-design:play-circle-outlined" :size="14" />
          加载数据源
        </a-button>
        <a-button v-if="dataSourceListSelectLength === 1" type="dashed" :style="{ 'color': '#cf1322' }" @click="dataSourceUninstallDataSource">
          <CoreIcon icon="fa6-regular:circle-stop" :size="14" />
          卸载数据源
        </a-button>
      </a-space>
      <a-space :style="{ ...coreStyle.floatRight }">
        <a-button type="dashed" :style="{ ...coreStyle.antBtnTemplateDownload }" @click="dataSourceTemplateDownload">
          <CoreIcon :icon="coreStyle.antBtnIconTemplateDownload" :size="14" />
          模版下载
        </a-button>
        <a-button type="dashed" :style="{ ...coreStyle.antBtnImport }" @click="dataSourceDataImport">
          <CoreIcon :icon="coreStyle.antBtnIconImport" :size="14" />
          导入
        </a-button>
        <a-button type="dashed" :style="{ ...coreStyle.antBtnExport }" @click="dataSourceDataExport">
          <CoreIcon :icon="coreStyle.antBtnIconExport" :size="14" />
          导出
        </a-button>
        <a-button type="dashed" ghost shape="circle" :style="{ ...coreStyle.antBtnRefresh }" @click="dataSourceListGetDataSet">
          <CoreIcon :icon="coreStyle.antBtnIconRefresh" :size="22" />
        </a-button>
      </a-space>
      <a-table
        rowKey="id"
        :columns="dataSourceListColumns"
        :data-source="dataSourceListDataSet"
        :loading="dataSourceListLoading"
        :pagination="false"
        bordered
        :customRow="dataSourceListCustomRow"
        :row-selection="{ fixed: true, selectedRowKeys: dataSourceListSelectedRowKeys, onChange: dataSourceListRowOnChange }"
        :scroll="{ x: '100%', y: 550 }"
        :style="{ ...coreStyle.marginTop(10) }"
      >
      </a-table>
      <a-pagination
        v-model:current="dataSourceListDataPage"
        v-model:pageSize="dataSourceListDataRows"
        :total="dataSourceListDataCount"
        show-size-changer
        show-quick-jumper
        :page-size-options="[ ...coreStyle.antdPaginationPageSizeOptions ]"
        :show-total="(total, range) => coreStyle.antdPaginationShowTotal(total, range)"
        :style="{ ...coreStyle.marginTop(10), ...coreStyle.textAlignCenter }"
        @change="dataSourceListPaginationChange"
        @showSizeChange="dataSourceListPaginationShowSizeChange"
        v-if="dataSourceListPaginationVisible"
      >
      </a-pagination>
      <p :style="{ ...coreStyle.marginTop(14), ...coreStyle.marginBottom(6), ...coreStyle.textAlignCenter }" v-if="!dataSourceListPaginationVisible">
        {{ coreStyle.antdPaginationShowTotal(dataSourceListDataCount) }}
      </p>
    </a-card>
    <a-drawer
      v-model:visible="dataSourceEditDrawerVisible"
      title="编辑数据源信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
      @close="dataSourceEditDrawerOnClose"
    >
      <a-form ref="dataSourceFormRef" :model="dataSourceFormModel" :rules="dataSourceFormRules" labelAlign="left" layout="vertical">
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="数据源名称" name="dataSourceName">
              <a-input v-model:value="dataSourceFormModel.dataSourceName" placeholder="输入数据源名称" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数据源标识" name="dataSourceKey">
              <a-input v-model:value="dataSourceFormModel.dataSourceKey" placeholder="输入数据源标识" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数据库类型" name="dbType">
              <a-input v-model:value="dataSourceFormModel.dbType" placeholder="输入数据库类型" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="IP地址" name="ipAddress">
              <a-input v-model:value="dataSourceFormModel.ipAddress" placeholder="输入IP地址" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="端口" name="port">
              <a-input v-model:value="dataSourceFormModel.port" placeholder="输入端口" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="服务名" name="serverName">
              <a-input v-model:value="dataSourceFormModel.serverName" placeholder="输入服务名" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数据库名" name="dbName">
              <a-input v-model:value="dataSourceFormModel.dbName" placeholder="输入数据库名" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用户名" name="username">
              <a-input v-model:value="dataSourceFormModel.username" placeholder="输入用户名" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="密码" name="password">
              <a-input v-model:value="dataSourceFormModel.password" placeholder="输入密码" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="描述" name="description">
              <a-input v-model:value="dataSourceFormModel.description" placeholder="输入描述" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="排序权重" name="weightOrder">
              <a-input-number v-model:value="dataSourceFormModel.weightOrder" :style="{ ...coreStyle.width('100%') }" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" :style="{ ...coreStyle.antBtnReset }" @click="dataSourceFormReset">
            <CoreIcon :icon="coreStyle.antBtnIconReset" :size="14" />
            重置
          </a-button>
          <a-button :loading="dataSourceFormSaveSubmitting" type="dashed" :style="{ ...coreStyle.antBtnNextAdd }" @click="dataSourceFormNextAdd">
            <CoreIcon :icon="coreStyle.antBtnIconNextAdd" :size="14" />
            继续录入
          </a-button>
          <a-button :loading="dataSourceFormSaveSubmitting" type="dashed" :style="{ ...coreStyle.antBtnSave }" @click="dataSourceFormSave">
            <CoreIcon :icon="coreStyle.antBtnIconSave" :size="14" />
            保存
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="dataSourceDetailDrawerVisible"
      title="数据源详细信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
    >
      <a-space direction="vertical" :size="20" :style="{ ...coreStyle.width('100%') }">
        <a-descriptions title="基础信息" :column="2" bordered>
          <a-descriptions-item label="数据源名称" :span="1">{{ dataSourceDetailModel.dataSourceName }}</a-descriptions-item>
          <a-descriptions-item label="数据源标识" :span="1">{{ dataSourceDetailModel.dataSourceKey }}</a-descriptions-item>
          <a-descriptions-item label="数据库类型" :span="1">{{ dataSourceDetailModel.dbType }}</a-descriptions-item>
          <a-descriptions-item label="IP地址" :span="1">{{ dataSourceDetailModel.ipAddress }}</a-descriptions-item>
          <a-descriptions-item label="端口" :span="1">{{ dataSourceDetailModel.port }}</a-descriptions-item>
          <a-descriptions-item label="服务名" :span="1">{{ dataSourceDetailModel.serverName }}</a-descriptions-item>
          <a-descriptions-item label="数据库名" :span="1">{{ dataSourceDetailModel.dbName }}</a-descriptions-item>
          <a-descriptions-item label="用户名" :span="1">{{ dataSourceDetailModel.username }}</a-descriptions-item>
          <a-descriptions-item label="密码" :span="1">{{ dataSourceDetailModel.password }}</a-descriptions-item>
          <a-descriptions-item label="描述" :span="1">{{ dataSourceDetailModel.description }}</a-descriptions-item>
          <a-descriptions-item label="排序权重" :span="1">{{ dataSourceDetailModel.weightOrder }}</a-descriptions-item>
        </a-descriptions>
      </a-space>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" :style="{ ...coreStyle.antBtnUpdate }" @click="dataSourceDetailUpdate">
            <CoreIcon :icon="coreStyle.antBtnIconUpdate" :size="14" />
            修改
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="dataSourceDataImportDrawerVisible"
      title="数据源数据导入"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 50, 0) }"
    >
      <a-row :gutter="16">
        <a-col :span="24">
          <FileUpload ref="dataSourceDataImportFileUploadRef" :fileModule="dataSourceDataImportFileModule" @change="dataSourceDataImportFileUploadChange" />
          <a-input v-model:value="dataSourceDataImportFileUrl" disabled addonBefore="导入文件url:" placeholder="输入导入文件url">
            <template #addonAfter>
              <a-button type="link" size="small" @click="dataSourceDataImportFileUrlSelectFileClick">选择文件</a-button>
            </template>
          </a-input>
        </a-col>
      </a-row>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button :loading="dataSourceDataImportSaveSubmitting" type="dashed" :style="{ ...coreStyle.antBtnImportData }" @click="dataSourceToDataImport">
            <CoreIcon :icon="coreStyle.antBtnIconImportData" :size="14" />
            导入数据
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="dataSourceDataImportFileSelectDrawerVisible"
      title="文件选择"
      placement="right"
      :closable="false"
      destroyOnClose
      :width="'80%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 0, 0) }"
    >
      <FileSelect :fileModule="dataSourceDataImportFileModule" @change="dataSourceDataImportFileSelectChange" />
    </a-drawer>
    <a-drawer
      v-model:visible="dataSourceDataExportDrawerVisible"
      title="数据源数据导出"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 50, 0) }"
    >
      <DataExportColumnSelect :optionalDataExportFieldList="dataSourceOptionalDataExportFieldList" :selectedDataExportFieldList="dataSourceSelectedDataExportFieldList" />
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" :style="{ ...coreStyle.antBtnQueryDataExport }" @click="dataSourceToDataExport(false)">
            <CoreIcon :icon="coreStyle.antBtnIconQueryDataExport" :size="14" />
            查询数据导出
          </a-button>
          <a-button type="dashed" :style="{ ...coreStyle.antBtnAllDataExport }" @click="dataSourceToDataExport(true)">
            <CoreIcon :icon="coreStyle.antBtnIconAllDataExport" :size="14" />
            全部数据导出
          </a-button>
        </a-space>
      </div>
    </a-drawer>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component'
import { mapStores } from 'pinia'
import { useCoreToolStore } from '/@/store/modules/cn/core/tool/coreToolStore'
import type { Res } from '/@/tool/Res'
import type { Param } from '/@/tool/Param'

import type { DataExportFieldAO } from '/@/model/cn/core/tool/ao/DataExportFieldAO'
import type { DataExportAO } from '/@/model/cn/core/tool/ao/DataExportAO'
import { toDataExportAO } from '/@/model/cn/core/tool/ao/DataExportAO'

import { useDataSourceStore } from '/@/store/modules/cn/core/kpis/dataSourceStore'

import type { DataSource } from '/@/model/cn/core/kpis/eo/DataSource'
import { toDataSource } from '/@/model/cn/core/kpis/eo/DataSource'

import type { DataSourceQO } from '/@/model/cn/core/kpis/qo/DataSourceQO'
import { toDataSourceQO } from '/@/model/cn/core/kpis/qo/DataSourceQO'

import type { DataSourceVO } from '/@/model/cn/core/kpis/vo/DataSourceVO'
import { toDataSourceVO } from '/@/model/cn/core/kpis/vo/DataSourceVO'

@Options({
  name: 'DataSource',
  components: {
  },
  computed: {
    ...mapStores(useCoreToolStore, useDataSourceStore),
  },
})
export default class DataSource extends Vue {

  dataSourceListDataSet: Array<DataSourceVO> = []

  dataSourceListColumns: Array<object> = [
    {
      title: '数据源名称',
      dataIndex: 'dataSourceName',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '数据源标识',
      dataIndex: 'dataSourceKey',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '数据库类型',
      dataIndex: 'dbType',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: 'IP地址',
      dataIndex: 'ipAddress',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '端口',
      dataIndex: 'port',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '服务名',
      dataIndex: 'serverName',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '数据库名',
      dataIndex: 'dbName',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '用户名',
      dataIndex: 'username',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '密码',
      dataIndex: 'password',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '描述',
      dataIndex: 'description',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    {
      title: '排序权重',
      dataIndex: 'weightOrder',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
  ]

  dataSourceListLoading: boolean = false

  dataSourceListSelectedRowKeys: string[] = []

  dataSourceListSelectData: Array<DataSourceVO> = []

  dataSourceListSelectLength: number = 0

  dataSourceListSelectCurrentRowIndex: number = -1

  dataSourceListSelectCurrentRowData: DataSourceVO = toDataSourceVO()

  dataSourceListPaginationVisible: boolean = true

  dataSourceListDataPage: number = 1

  dataSourceListDataRows: number = 10

  dataSourceListDataCount: number = 0

  dataSourceListRowClickTimeout: number | null = null

  dataSourceListRowOnChange(selectedRowKeys, selectedRows) {
    this.dataSourceListSelectedRowKeys = [ ...selectedRowKeys ]
    this.dataSourceListSelectData = [ ...selectedRows ]
    this.dataSourceListSelectLength = selectedRows.length
  }

  dataSourceListCustomRow(record, index) {
    return {
      'class': index === this.dataSourceListSelectCurrentRowIndex ? 'ant-table-row-selected' : '',
      onClick: (event) => {
        if (this.dataSourceListRowClickTimeout) {
          clearTimeout(this.dataSourceListRowClickTimeout)
          this.dataSourceListRowClickTimeout = null
        } else {
          this.dataSourceListRowClickTimeout = setTimeout(() => {
            this.dataSourceListRowClickTimeout = null
            this.dataSourceListSelectCurrentRowIndex = index
            this.dataSourceListSelectCurrentRowData = { ...record }
            let selectedRowKeys = [ ...this.dataSourceListSelectedRowKeys ]
            let selectedRows = [ ...this.dataSourceListSelectData ]
            if (selectedRowKeys.includes(record.id)) {
              this.dataSourceListRowOnChange([ ...selectedRowKeys.filter(eo => { return eo !== record.id }) ], [ ...selectedRows.filter(eo => { return eo.id !== record.id }) ])
            } else {
              selectedRowKeys.push(record.id)
              selectedRows.push(record)
              this.dataSourceListRowOnChange([ ...new Set(selectedRowKeys) ], [ ...new Set(selectedRows) ])
            }
          }, 200)
        }
      },
      onDblclick: (event) => {
        this.dataSourceStore.pkVo(this.$toParam<DataSourceQO>({ id: record.id } as DataSourceQO)).then((res: Res<DataSourceVO>) => {
          if (res.succ) {
            this.dataSourceDetailModel = { ...res.data[0] }
            this.dataSourceDetailDrawerVisible = true
          } else {
            this.$message.error(res.msg, 6)
          }
        })
      },
    }
  }

  dataSourceListGetDataSet() {
    this.dataSourceListLoading = true
    this.dataSourceListRowOnChange([], [])
    this.dataSourceListSelectCurrentRowIndex = -1
    this.dataSourceListSelectCurrentRowData = toDataSourceVO()
    this.dataSourceStore.pagingVo(this.$toParam<DataSourceQO>({ weightOrderAsc: 'y' } as DataSourceQO, this.dataSourceListDataPage, this.dataSourceListDataRows)).then((res: Res<DataSourceVO>) => {
      if (res.succ) {
        this.dataSourceListPaginationVisible = true
        this.dataSourceListDataSet = [ ...res.data ]
        this.dataSourceListDataCount = res.count
        setTimeout(() => { this.dataSourceListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.dataSourceListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.dataSourceListLoading = false }, 500)
    })
  }

  dataSourceListPaginationChange() {
    this.dataSourceListGetDataSet()
  }

  dataSourceListPaginationShowSizeChange() {
    this.dataSourceListGetDataSet()
  }

  dataSourceFormQueryVisible: boolean = false

  dataSourceFormQuerySubmitting: boolean = false

  dataSourceFormQueryModel: DataSourceQO = toDataSourceQO()

  dataSourceFormQueryUpDown() {
    this.dataSourceFormQueryVisible = !this.dataSourceFormQueryVisible
  }

  dataSourceFormQueryReset() {
    if (this.$refs.dataSourceFormQueryRef) {
      this.$refs.dataSourceFormQueryRef.resetFields()
      this.dataSourceFormQueryModel = toDataSourceQO()
    }
  }

  dataSourceFormQuerySearch() {
    this.dataSourceFormQuerySubmitting = true
    this.dataSourceListLoading = true
    this.dataSourceListRowOnChange([], [])
    this.dataSourceListSelectCurrentRowIndex = -1
    this.dataSourceListSelectCurrentRowData = toDataSourceVO()
    this.dataSourceStore.queryVo(this.$toParam<DataSourceQO>(this.$toSearch({ ...this.dataSourceFormQueryModel }))).then((res: Res<DataSourceVO>) => {
      if (res.succ) {
        this.dataSourceListPaginationVisible = false
        this.dataSourceListDataSet = [ ...res.data ]
        this.dataSourceListDataCount = res.count
        setTimeout(() => { this.dataSourceFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.dataSourceListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.dataSourceFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.dataSourceListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.dataSourceFormQuerySubmitting = false }, 5000)
      setTimeout(() => { this.dataSourceListLoading = false }, 500)
    })
  }

  dataSourceEditDrawerVisible: boolean = false

  dataSourceFormSaveSubmitting: boolean = false

  dataSourceFormSaveContinue: boolean = false

  dataSourceFormModel: DataSource = toDataSource()

  dataSourceFormRules: object = {
    // dataSourceName: [
    //   { type: 'string', required: true, message: '数据源名称不能为空！', trigger: ['change', 'blur'] },
    // ],
    // dataSourceKey: [
    //   { type: 'string', required: true, message: '数据源标识不能为空！', trigger: ['change', 'blur'] },
    // ],
    // dbType: [
    //   { type: 'string', required: true, message: '数据库类型不能为空！', trigger: ['change', 'blur'] },
    // ],
    // ipAddress: [
    //   { type: 'string', required: true, message: 'IP地址不能为空！', trigger: ['change', 'blur'] },
    // ],
    // port: [
    //   { type: 'string', required: true, message: '端口不能为空！', trigger: ['change', 'blur'] },
    // ],
    // serverName: [
    //   { type: 'string', required: true, message: '服务名不能为空！', trigger: ['change', 'blur'] },
    // ],
    // dbName: [
    //   { type: 'string', required: true, message: '数据库名不能为空！', trigger: ['change', 'blur'] },
    // ],
    // username: [
    //   { type: 'string', required: true, message: '用户名不能为空！', trigger: ['change', 'blur'] },
    // ],
    // password: [
    //   { type: 'string', required: true, message: '密码不能为空！', trigger: ['change', 'blur'] },
    // ],
    // description: [
    //   { type: 'string', required: true, message: '描述不能为空！', trigger: ['change', 'blur'] },
    // ],
    // weightOrder: [
    //   { type: 'number', required: true, message: '排序权重不能为空！', trigger: ['change', 'blur'] },
    // ],
  }

  dataSourceFormReset() {
    if (this.$refs.dataSourceFormRef) {
      this.$refs.dataSourceFormRef.resetFields()
      this.dataSourceFormModel = toDataSource()
    }
  }

  dataSourceFormSubmit() {
    this.dataSourceFormSaveSubmitting = true
    this.$refs.dataSourceFormRef.validate().then(() => {
      if (this.dataSourceFormModel.id) {
        this.dataSourceStore.update(this.$toParam<DataSource>(this.dataSourceFormModel)).then((res: Res<DataSource>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.dataSourceFormReset()
            this.dataSourceFormSaveSubmitting = false
            if (!this.dataSourceFormSaveContinue) {
              this.dataSourceEditDrawerVisible = false
              this.dataSourceListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.dataSourceFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.dataSourceFormSaveSubmitting = false }, 3000)
        })
      } else {
        this.dataSourceStore.save(this.$toParam<DataSource>(this.dataSourceFormModel)).then((res: Res<DataSource>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.dataSourceFormReset()
            this.dataSourceFormSaveSubmitting = false
            if (!this.dataSourceFormSaveContinue) {
              this.dataSourceEditDrawerVisible = false
              this.dataSourceListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.dataSourceFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.dataSourceFormSaveSubmitting = false }, 3000)
        })
      }
    }).catch(() => {
      setTimeout(() => { this.dataSourceFormSaveSubmitting = false }, 3000)
    })
  }

  dataSourceFormSave() {
    this.dataSourceFormSaveContinue = false
    this.dataSourceFormSubmit()
  }

  dataSourceFormNextAdd() {
    this.dataSourceFormSaveContinue = true
    this.dataSourceFormSubmit()
  }

  dataSourceAdd() {
    if (this.dataSourceFormModel.id) {
      this.dataSourceFormReset()
    }
    this.dataSourceEditDrawerVisible = true
  }

  dataSourceUpdate() {
    this.dataSourceStore.pk(this.$toParam<DataSourceQO>({ id: this.dataSourceListSelectData[0].id } as DataSourceQO)).then((res: Res<DataSource>) => {
      if (res.succ) {
        this.dataSourceFormReset()
        this.dataSourceFormModel = { ...res.data[0] }
        this.dataSourceEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dataSourceEditDrawerOnClose() {
    if (this.dataSourceFormSaveContinue) {
      this.dataSourceFormSaveContinue = false
      this.dataSourceListGetDataSet()
    }
  }

  dataSourceDelete() {
    let param: Array<DataSource> = []
    this.dataSourceListSelectData.forEach((eo: DataSourceVO) => {
      param.push({ id: eo.id } as DataSource)
    })
    this.dataSourceStore.removeBatch(this.$toParam<DataSource>(param)).then((res: Res<DataSource>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.dataSourceListGetDataSet()
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dataSourceDetailDrawerVisible: boolean = false

  dataSourceDetailModel: DataSourceVO = toDataSourceVO()

  dataSourceDetailInfo() {
    this.dataSourceStore.pkVo(this.$toParam<DataSourceQO>({ id: this.dataSourceListSelectData[0].id } as DataSourceQO)).then((res: Res<DataSourceVO>) => {
      if (res.succ) {
        this.dataSourceDetailModel = { ...res.data[0] }
        this.dataSourceDetailDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dataSourceDetailUpdate() {
    this.dataSourceStore.pk(this.$toParam<DataSourceQO>({ id: this.dataSourceDetailModel.id } as DataSourceQO)).then((res: Res<DataSource>) => {
      if (res.succ) {
        this.dataSourceFormReset()
        this.dataSourceFormModel = { ...res.data[0] }
        this.dataSourceDetailDrawerVisible = false
        this.dataSourceEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dataSourceAddDataSource() {
    this.dataSourceStore.addDataSource(this.$toParam<DataSource>({ id: this.dataSourceListSelectData[0].id } as DataSource)).then((res: Res<DataSource>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dataSourceUninstallDataSource() {
    this.dataSourceStore.uninstallDataSource(this.$toParam<DataSource>({ id: this.dataSourceListSelectData[0].id } as DataSource)).then((res: Res<DataSource>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dataSourceTemplateDownload() {
    this.coreToolStore.getValueByConfKey('defaultDataImportTemplate').then((res: string) => {
      window.open(this.coreToolStore.getFileFullUrl(res), '_blank')
    })
  }

  dataSourceDataImportDrawerVisible: boolean = false

  dataSourceDataImportFileSelectDrawerVisible: boolean = false

  dataSourceDataImportFileModule: string = 'dataImport'

  dataSourceDataImportFileUrl: string = ''

  dataSourceDataImportSaveSubmitting: boolean = false

  dataSourceDataImport() {
    this.dataSourceDataImportDrawerVisible = true
  }

  dataSourceDataImportFileUploadChange(value) {
    this.dataSourceDataImportFileUrl = value
  }

  dataSourceDataImportFileUrlSelectFileClick() {
    this.dataSourceDataImportFileSelectDrawerVisible = true
  }

  dataSourceDataImportFileSelectChange(fileUrl) {
    this.dataSourceDataImportFileUrl = fileUrl
    this.dataSourceDataImportFileSelectDrawerVisible = false
  }

  dataSourceToDataImport() {
    this.dataSourceDataImportSaveSubmitting = true
    this.dataSourceStore.dataImport(this.$toParam<string>(this.dataSourceDataImportFileUrl)).then((res: Res<DataSource>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.$refs.dataSourceDataImportFileUploadRef.resetFiles()
        this.dataSourceDataImportFileUrl = ''
        this.dataSourceDataImportSaveSubmitting = false
        this.dataSourceDataImportDrawerVisible = false
        this.dataSourceListGetDataSet()
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.dataSourceDataImportSaveSubmitting = false }, 3000)
      }
    }).catch(err => {
      setTimeout(() => { this.dataSourceDataImportSaveSubmitting = false }, 3000)
    })
  }

  dataSourceDataExportDrawerVisible: boolean = false

  dataSourceOptionalDataExportFieldList: Array<DataExportFieldAO> = [
    {
      fieldName: 'dataSourceName',
      fieldComment: '数据源名称',
    },
    {
      fieldName: 'dataSourceKey',
      fieldComment: '数据源标识',
    },
    {
      fieldName: 'dbType',
      fieldComment: '数据库类型',
    },
    {
      fieldName: 'ipAddress',
      fieldComment: 'IP地址',
    },
    {
      fieldName: 'port',
      fieldComment: '端口',
    },
    {
      fieldName: 'serverName',
      fieldComment: '服务名',
    },
    {
      fieldName: 'dbName',
      fieldComment: '数据库名',
    },
    {
      fieldName: 'username',
      fieldComment: '用户名',
    },
    {
      fieldName: 'password',
      fieldComment: '密码',
    },
    {
      fieldName: 'description',
      fieldComment: '描述',
    },
    {
      fieldName: 'weightOrder',
      fieldComment: '排序权重',
    },
  ]

  dataSourceSelectedDataExportFieldList: Array<DataExportFieldAO> = []

  dataSourceDataExport() {
    this.dataSourceDataExportDrawerVisible = true
  }

  dataSourceToDataExport(allData: boolean) {
    this.dataSourceStore.dataExport(this.$toParam<DataExportAO<DataSourceQO>>(toDataExportAO(this.dataSourceSelectedDataExportFieldList, allData ? { weightOrderAsc: 'y' } as DataSourceQO : this.$toSearch({ ...this.dataSourceFormQueryModel }), '数据源信息数据导出.xlsx'))).then((res: Res<string>) => {
      if (res.succ) {
        window.open(this.coreToolStore.getFileFullUrl(res.data[0]), '_blank')
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  mounted() {
    // this.dataSourceFormQueryModel.weightOrderAsc = 'y'
    this.dataSourceListGetDataSet()
  }

}
</script>

<style scoped lang="less">

</style>
